#include "CGameState.h"

#include "../core/CRetroInput.h"
#include "../core/CDirect3D.h"
#include "CStateMachine.h"
#include "../CGame.h"
#include "../gameobjects/CObjectManager.h"
#include "../gameobjects/CEnemySpawner.h"

CGameState::CGameState(void)
{
	enemySpawner = NULL;
}


CGameState::~CGameState(void)
{
}


void CGameState::Enter()
{
	displayChar[1] = '\0';
	
	m_pObjMan = CObjectManager::GetInstance();
	m_pObjMan->Initialize(&m_Camera);

	m_pPlayer = m_pObjMan->CreatePlayerObject(0.0f, 0.0f);
	m_Camera.Initialize(CGame::GetInstance()->m_nWindowWidth, CGame::GetInstance()->m_nWindowHeight, (CBaseObject*)m_pPlayer);
	
}

void CGameState::Input(CRetroInput* pInput)
{
	if(pInput->KeyReleased(DIK_ESCAPE))
	{
		static bool adding = true;
		if(adding)
		{
			enemySpawner = (CEnemySpawner*)m_pObjMan->CreateEnemy(0, 0, ENEMY_SPAWNER);
		}
		else
		{
			m_pObjMan->RemoveObject((IBaseObject*)enemySpawner);
			enemySpawner = NULL;
		}

		adding = !adding;
	}

	m_pObjMan->Input(pInput);
}

void CGameState::Update(float fDeltaTime)
{	
	m_pObjMan->Update(fDeltaTime);
	m_Camera.Update(fDeltaTime);
}

void CGameState::Render(CDirect3D* pRender)
{
	m_pObjMan->Render(pRender);
	const vec3<float>& cursor = CGame::GetInstance()->GetCursorPosition();
	pRender->DrawTextA(displayChar, static_cast<int>(cursor.x),  static_cast<int>(cursor.y), 0, 255, 0);
}

void CGameState::Exit()
{
	m_Camera.Shutdown();
	m_pObjMan->Shutdown();
	m_pObjMan->FreeInstance();
}
